﻿using Ninject;
using Ninject.Syntax;
using System;

namespace HobSoft.SoccerBet2014.Client.Factory
{
    using Domain.Core;
    using ViewModels;
    public interface IViewModelFactory : IServiceFactory
    {
        T CreateOrGet<T>() where T : ViewModelBase;
        ViewModelBase CreateOrGet(Type type);
    }

    public class ViewModelFactory : IViewModelFactory
    {
        private readonly IResolutionRoot _resolutionRoot;
        public ViewModelFactory(IResolutionRoot resolutionRoot)
        {
            _resolutionRoot = resolutionRoot;
        }

        public T CreateOrGet<T>() where T : ViewModels.ViewModelBase
        {
            return _resolutionRoot.Get<T>();
        }

        public ViewModelBase CreateOrGet(Type type)
        {
            if (!type.IsSubclassOf(typeof(ViewModelBase)))
                throw new ArgumentException("必须是ViewModelBase类型", "type");
            return (ViewModelBase)_resolutionRoot.Get(type);
        }

        public object CreateOrGet()
        {
            throw new NotSupportedException();
        }

    }
}
